package com.yunji.base.service;

import com.google.common.base.Optional;
import com.yunji.base.bo.ManagerUpdateBO;
import com.yunji.base.cache.BaseCache;
import com.yunji.base.convert.ManagerConvert;
import com.yunji.base.dao.IManagerDao;
import com.yunji.base.dao.IManagerSiteDao;
import com.yunji.base.dao.ISiteDao;
import com.yunji.base.domain.ManagerDO;
import com.yunji.base.domain.ManagerSiteDO;
import com.yunji.base.dto.ManagerDTO;
import com.yunji.common.model.domain.SiteDO;
import com.yunji.common.model.domain.SysUserDO;
import com.yunji.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * @version 1.0
 * @Project: yunji-coffee
 * @Package com.yunji.user.service.impl
 * @FileName ManagerServiceImpl.java
 * @Copyright Copyright © 2024 云集互联. All Rights Reserved
 * Company		com
 * @CreateTime 2024-05-29 17:07:18
 * @Description 网点用户服务接口实现
 * @Modification Author                    History Time			Version				Description
 * --------------------------------------------------------------------------------
 * HuaAo0803@163.com	2024-05-29 17:07:18		1.0					1.0 Version
 * @since JDK 1.8.0_202
 */
@Service
public class ManagerService {
    @Resource
    IManagerDao managerDao;

    @Resource
    private IManagerSiteDao managerSiteDao;

    @Resource
    private ISiteDao siteDao;

//    public List<Manager> search(Manager manager) {
//        return managerDao.search(manager);
//    }

    public List<ManagerDO> search(Long... userIds) {
        List<ManagerDO> managers = managerDao.listByUserIds(Arrays.asList(userIds));
        ManagerConvert.convert(managers);
        return managers;
    }

    public ManagerDO select(Long managerId) {
        ManagerDO byUserId = managerDao.getByUserId(managerId);
        ManagerConvert.convert(byUserId);
        return byUserId;
    }

    /**
     * @param agentId 代理ID数组
     * @Title searchAgent
     * @Desc 查询代理下经理用户信息列表
     * @Date 2024-05-30 17:33:02.122
     * @Return List<Manager>
     */

    public List<ManagerDO> searchAgent(Long... agentId) {
        List<ManagerDO> managers = managerDao.listByAgentIds(Arrays.asList(agentId));
        ManagerConvert.convert(managers);
        return managers;
    }

    /**
     * @param manager 经理用户信息
     * @Title create
     * @Desc 创建经理用户信息
     * @Date 2024-05-30 17:34:03.971
     * @Return boolean 创建结果
     */

    public boolean create(ManagerDO manager) {
        return managerDao.save(manager);
    }

    /**
     * @param manager 经理用户信息
     * @Title update
     * @Desc 更新经理用户信息
     * @Date 2024-05-30 17:34:36.183
     * @Return boolean 更新结果
     */

    public boolean update(ManagerDO manager) {
        return managerDao.updateById(manager);
    }

    /**
     * @param ids 经理用户数组
     * @Title delete
     * @Desc 删除经理用户信息
     * @Date 2024-05-30 17:34:39.090
     * @Return boolean 删除结果
     */

    public boolean delete(Long... ids) {
        return managerDao.removeBatchByIds(Arrays.asList(ids));
    }

    /**
     * @param userId 用户ID数组
     * @Title deleteByUserId
     * @Desc 根据用户ID删除经理信息
     * @Date 2024-06-01 17:30:55.617
     * @Return boolean 删除结果
     */
    public boolean deleteByUserId(Long... userId) {
        return managerDao.deleteByUserId(Arrays.asList(userId));
    }

    /**
     * @param agentId 代理商ID数组
     * @Title deleteByAgentId
     * @Desc 根据代理商ID删除经理信息
     * @Date 2024-06-01 17:32:26.157
     * @Return boolean 删除结果
     */

    public boolean deleteByAgentId(Long... agentId) {
        return managerDao.deleteByAgentId(Arrays.asList(agentId));
    }

    public ManagerDTO selectManagerUser(Long siteId) {
        ManagerDTO managerDTO = new ManagerDTO();
        SysUserDO user = Optional.fromNullable(BaseCache.getUser(siteId)).or(new SysUserDO());
        ManagerSiteDO managerSite = Optional.fromNullable(managerSiteDao.getByManagerId(siteId)).or(new ManagerSiteDO());
        SiteDO site = Optional.fromNullable(siteDao.getById(managerSite.getSiteId())).or(new SiteDO());
        ManagerDO manager = managerDao.getByUserId(managerSite.getManagerId());
        BeanUtils.clone(manager, managerDTO);
        managerDTO.setManagerName(user.getNickName());
        managerDTO.setManagerPhone(user.getPhonenumber());
        managerDTO.setSiteScaleDateS(site.getSiteScaleDates());
        managerDTO.setSiteScaleDateE(site.getSiteScaleDatee());
        return managerDTO;
    }


    public void updateScaleByManagerId(Long managerId, BigDecimal scale) {
        managerDao.updateScaleByMangerId(managerId, scale);
    }

    public boolean updateByManager(ManagerUpdateBO bo) {
        return managerDao.updateByManger(bo);
    }

    public boolean customPriceByManagerId(Long managerId, Integer customPrice) {
        return managerDao.customPriceByManagerId(managerId, customPrice);
    }
}